#!/usr/bin/env python
# coding:utf-8

import cyclone.auth
import cyclone.escape
import cyclone.web
from toughadmin.tools import utils
from toughadmin.console.admin.base import BaseHandler, MenuRes
from toughadmin.tools.permit import permit
from toughadmin.console import models
from toughadmin.console.admin import ikuai_form


class IkuaiHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        self.render("ikuai_list.html",
                    page_data=self.get_page_data(self.db.query(models.TraIkuai)))


class AddHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        domains = [(m.domain_code, m.domain_desc) for m in self.db.query(models.TraDomain)]
        self.render("base_form.html", form=ikuai_form.ikuai_add_form(domains))

    @cyclone.web.authenticated
    def post(self):
        form = ikuai_form.ikuai_add_form([])
        if not form.validates(source=self.get_params()):
            return self.render("base_form.html", form=form)
        if self.db.query(models.TraIkuai.id).filter_by(nas_gwid=form.d.nas_gwid).count() > 0:
            return self.render("base_form.html", form=form, msg=u"gwid已经存在")
        new_ikuai = models.TraIkuai()
        new_ikuai.nas_name = form.d.nas_name
        new_ikuai.nas_ver = form.d.nas_ver
        new_ikuai.api_key = form.d.api_key
        new_ikuai.nas_addr = form.d.nas_addr
        new_ikuai.domain_code = form.d.domain_code
        new_ikuai.nas_gwid = form.d.nas_gwid
        new_ikuai.nas_desc = form.d.nas_desc
        self.db.add(new_ikuai)

        ops_log = models.TraOperateLog()
        ops_log.operator_name = self.get_secure_cookie("tra_user")
        ops_log.operate_ip = self.get_secure_cookie("tra_login_ip")
        ops_log.operate_time = utils.get_currtime()
        ops_log.operate_desc = u'操作员(%s)新增爱快设备:%s' % (ops_log.operator_name, new_ikuai.nas_gwid)
        self.db.add(ops_log)

        self.db.commit()
        self.redirect("/ikuai", permanent=False)


class UpdateHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        domains = [(m.domain_code, m.domain_desc) for m in self.db.query(models.TraDomain)]
        ikuai_id = self.get_argument("ikuai_id")
        nikuai = self.db.query(models.TraIkuai).get(ikuai_id)
        form = ikuai_form.ikuai_update_form(domains)
        form.fill(nikuai)
        return self.render("base_form.html", form=form)

    def post(self):
        form = ikuai_form.ikuai_update_form([])
        if not form.validates(source=self.get_params()):
            return self.render("base_form.html", form=form)

        mod_ikuai = self.db.query(models.TraIkuai).get(form.d.id)
        mod_ikuai.nas_name = form.d.nas_name
        mod_ikuai.nas_ver = form.d.nas_ver
        mod_ikuai.api_key = form.d.api_key
        mod_ikuai.nas_addr = form.d.nas_addr
        mod_ikuai.domain_code = form.d.domain_code
        mod_ikuai.nas_desc = form.d.nas_desc

        ops_log = models.TraOperateLog()
        ops_log.operator_name = self.get_secure_cookie("tra_user")
        ops_log.operate_ip = self.get_secure_cookie("tra_login_ip")
        ops_log.operate_time = utils.get_currtime()
        ops_log.operate_desc = u'操作员(%s)修改爱快设备:%s' % (ops_log.operator_name, mod_ikuai.nas_gwid)
        self.db.add(ops_log)

        self.db.commit()
        self.redirect("/ikuai", permanent=False)


class DeleteHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        ikuai_id = self.get_argument("ikuai_id")
        self.db.query(models.TraIkuai).filter_by(id=ikuai_id).delete()

        ops_log = models.TraOperateLog()
        ops_log.operator_name = self.get_secure_cookie("tra_user")
        ops_log.operate_ip = self.get_secure_cookie("tra_login_ip")
        ops_log.operate_time = utils.get_currtime()
        ops_log.operate_desc = u'操作员(%s)删除爱快设备:%s' % (ops_log.operator_name, ikuai_id)
        self.db.add(ops_log)

        self.db.commit()
        self.redirect("/ikuai", permanent=False)


permit.add_route(IkuaiHandler, r"/ikuai", u"爱快信息管理", MenuRes, order=2.0710, is_menu=True)
permit.add_route(AddHandler, r"/ikuai/add", u"爱快信息新增", MenuRes, order=2.0711, is_menu=False)
permit.add_route(UpdateHandler, r"/ikuai/update", u"爱快信息修改", MenuRes, order=2.0712, is_menu=False)
permit.add_route(DeleteHandler, r"/ikuai/delete", u"爱快信息删除", MenuRes, order=2.0713, is_menu=False)